package io.renren.modules.three;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;

import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        new Test().getFileList(100, "paizhao");
    }

    public List<OSSObjectSummary> getFileList(int num, String filePath){
        OSSClient client = new OSSClient(Constants.endpoint, Constants.accessKeyId, Constants.accessKeySecret);
        List<OSSObjectSummary> sums = new ArrayList<>();
        try {
            ObjectListing objectListing = client.listObjects(new ListObjectsRequest(Constants.bucketName)
                    .withMaxKeys(num).withPrefix(filePath));
            sums = objectListing.getObjectSummaries();
            for (OSSObjectSummary s : sums) {
                System.out.println("\t" + s.getKey());
            }
        } catch (OSSException oe) {
            System.out.println("Error Message: " + oe.getErrorCode());
            System.out.println("Error Code: " + oe.getErrorCode());
            System.out.println("Request ID: " + oe.getRequestId());
            System.out.println("Host ID: " + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Error Message: " + ce.getMessage());
        } finally {
            client.shutdown();
        }
        return sums;
    }

}
